(function($){

    window.ServiceUnit = {
        request:function(option){
            var config = {
                 "serviceName":null
                ,"methodName":null
                ,"params":[]
                ,"token":sessionStorage.getItem("token")
                ,"timeout":30000
                ,"async":false
                ,"callback":null
            };

            config = $.extend(config,option);

            $.ajax({
                url:"/"+config.serviceName+"/"+config.methodName,
                data:JSON.stringify(config.params),
                contentType:"application/json;charset=UTF-8",
                headers:{'token':config.token},
                type:"POST",
                timeout:config.timeout,
                async:config.async,
                dataType:"json",
                success:function(result,textStatus,jqXHR){
                    if(result && result.success){
                        if(result.status == "Redirect"){
                            if(window.parent){
                                window.parent.location.href = result.data;
                            }else{
                                location.href = result.data;
                            }
                        }
                        if(config.callback){
                            config.callback(result);
                        }
                    }else{
                    	$.messager.show({
							title:'提示信息',
							msg:result.exceptionMessage,
							iconCls:'icon-no',
							timeout:3000,
							showType:'fade',
							closable:false
						});
                    	if(ServiceUnit.debug){
                    		exceptionWindow=window.open('','','width=500,height=600');
                    		exceptionWindow.document.write("<title>错误信息</title>");
                            exceptionWindow.document.write("消息："+result.exceptionMessage);
                            exceptionWindow.document.write("<br\>");
                            exceptionWindow.document.write("栈信息："+result.exceptionStackTrace);
                    	}
                    }
                },
                error:function(jqXHR,textStatus,errorThrown){
                	 exceptionWindow=window.open('','','width=500,height=600');
                	 exceptionWindow.document.write(textStatus);
                	 exceptionWindow.document.write(errorThrown);
                }
            });
        },
        
        execute(serviceName,methodName,params,callback){
        	this.request({
                "serviceName":serviceName
                ,"methodName":methodName
                ,"params":params
                ,"callback":callback
            });
        },
        
        save:function (serviceName,data,callback) {
            this.request({
                "serviceName":serviceName
                ,"methodName":"save"
                ,"params":[data]
                ,"callback":callback
            });
        },
        delete:function(serviceName,data,callback){
            this.request({
                "serviceName":serviceName
                ,"methodName":"delete"
                ,"params":[data]
                ,"callback":callback
            });
        },
        query:function (serviceName,filterOption,paths,callback) {
        	var params = [filterOption];
        	if(paths){
        		params.push(paths);
        	}
            this.request({
                "serviceName":serviceName
                ,"methodName":"query"
                ,"params":[this.filter(filterOption),paths]
                ,"callback":callback
            });
        },

        filter:function (option) {
            var config = {
                 "mode":"and"
                ,"conditions":null //{"propertyName":"name","operate":"eq",value:[]}
                ,"pageNumber":null
                ,"pageSize":null
                ,"order":null
            };
            config = $.extend(config,option);
            var filter = {"where":{}}
            filter.where[config.mode] = conditions = [];
            if(config.conditions){
                for(var i=0;i<config.conditions.length;i++){
                    var condition = {} //{"name":{"eq":"a"}}
                    var operation = condition[config.conditions[i].propertyName]={};
                    operation[config.conditions[i].operate] = config.conditions[i].value;
                    conditions.push(condition)
                }
            }
            if(config.pageNumber && config.pageSize){
                var page = {"pageNumber":config.pageNumber,"pageSize":config.pageSize};
                filter["page"] = page
            }
            if(config.order){
                filter["order"] = config.order;
            }

            return JSON.stringify(filter);
        },
        
       queryServiceUnit: function(filterOption){
    	   	var list = [];
    	    var option = {
                    "serviceName":"ServiceUnitService"
                   ,"methodName":"queryFromXml"
                   ,"params":[ServiceUnit.filter(filterOption)]
                   ,"async":false
                   ,"callback":function(result){
                	   var serviceList = result.data;
	            	   for(var i=0;i<serviceList.length;i++){
	            		   var serviceName = serviceList[i].name;
	            		   serviceList[i].serviceName = serviceName;
	            		   serviceList[i].state = 'closed';
	            		   serviceList[i].id = serviceName;
	            		   for(var j=0;j<serviceList[i].methods.length;j++){
	            			   var methodName = serviceList[i].methods[j].name;
	            			   serviceList[i].methods[j].methodName = methodName;
	            			   serviceList[i].methods[j].id = serviceName+"_"+methodName;
	            			   
	            		   }
	            		   serviceList[i].children = serviceList[i].methods;
	            	   }
	            	   list = serviceList;
                   }
               };
      		ServiceUnit.request(option);
      		return list;
       },
       debug:false
        
    }
    
    
})(jQuery);